/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-05-18 21:55:48
 * @LastEditTime: 2022-07-18 22:24:34
 * @LastEditors: liutq
 * @Reference:
 */
package goods

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/maxyang107/jinglan/common"
	"github.com/maxyang107/jinglan/service"
	"github.com/maxyang107/jinglan/utils"
)

/**
 * @description: 方法描述：获取商品列表
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func GetGoodsList(c *gin.Context) {
	var listquery common.GoodsList

	if err := c.BindQuery(&listquery); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}
	gl := service.GoodsService.GetGoodsList(listquery)
	common.ResponseListToJson(http.StatusOK, int(gl.Page), int(gl.PerPage), int(gl.Total), gl.Data, c)
}

/**
 * @description: 方法描述：获取产品详情
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func GetGoodsDetail(c *gin.Context) {
	var param_ common.Detail
	if err := c.BindQuery(&param_); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}
	userid_, exists := c.Get("UserId")
	if !exists {
		common.ResponseToJson(http.StatusOK, common.NotAuth, "token解析失败", c)
		return
	}

	UserId := utils.InterfaceToUser(userid_)

	gt := service.GoodsService.GetGoodsDetail(param_.Id, UserId)
	if gt.ID > 0 {
		common.ResponseDetailToJson(http.StatusOK, gt, "success", c)
		return
	}

	common.ResponseToJson(http.StatusNotFound, common.SourceNotExist, "资源不存在或已下架", c)
}

/**
 * @description: 方法描述：获取抽签列表
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func GetGoodsListForDraw(c *gin.Context) {
	var listquery common.GoodsList

	if err := c.BindQuery(&listquery); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	userid, exists := c.Get("UserId")
	if !exists {
		common.ResponseToJson(http.StatusOK, common.NotAuth, "token解析失败", c)
		return
	}

	listquery.UserId = utils.InterfaceToUser(userid)
	gl := service.GoodsService.GetGoodsListForDraw(listquery)
	common.ResponseListToJson(http.StatusOK, int(gl.Page), int(gl.PerPage), int(gl.Total), gl.Data, c)
}

func Synchronize(c *gin.Context) {
	service.GoodsService.Synchronize()
	common.ResponseToJson(http.StatusOK, common.Ok, "同步成功", c)
}
